/**
 * 权限菜单管理
 * Created by Administrator on 2018/5/17.
 */
/**
 * Created by Administrator on 2018/5/15.
 */

define(function (require,exports,module) {
    var sTpl = require("./fboModuleMgr.html");
    require("./fboModuleMgr.css");

    var VueComponent = Vue.extend({
        template: sTpl
        ,methods:{
            getTopMenu:function(){
                axios.get(kunApis.menu+"/subModule?parentCode=02").then(function(result){
                    result.data.forEach(function(value,index,array){
                        value['child_num'] = 1;    //通过remote方式获取子树时，不确定有多少个子元素时可以先将子元素个数设置为一个大于0的数使其渲染为目录，当真正获取时子元素会根据实际子元素个数判断渲染为目录还是叶子节点
                    });
                    this.menus = result.data;
                }.bind(this));
            },
            addTopMenu:function(){
                this.resetMenuForm();
                this.menuFormData.moduleType="-1";
                this.menuFormData.parentCode="02";
                this.showDialog();
            },
            editMenuAuth:function(){
                this.$kun.openTab('02-bziPV06l-l6mk8NHL-G45LVSMz','角色管理','ftl/sysmgr/FwRoleMgrN',null);
            },
            getSubMenu:function(row,callback){
                axios.get(kunApis.menu+"/subModule?parentCode="+row.moduleCode).then(function(result){
                    result.data.forEach(function(value,index,array){
                        value['child_num'] = 1;
                    });
                    callback(result.data);
                }.bind(this))
            },
            showDialog:function(){
                this.dialogVisible = true;
            },
            resetMenuForm:function(){
                this.menuFormData = {
                    moduleId:null,
                    moduleCode:null,
                    parentCode:null,
                    moduleName:"",
                    moduleUrl:"",
                    moduleType:"",
                    moduleImg:"fa fa-file-text-o",
                    moduleStatus:"1",
                    remark:"",
                    moduleSeq:0
                };
                this.showIcons = false;
            },
            onSearchFwModule:function(val){
                var  searchVal =this.searchKey||"";
                this.urls.url =this.contextPath+this.urls.initurl+"?key="+searchVal;

            },
            getMenuType:function(){
                axios.get(kunApis.systemDim+"?level=1&filter=menuType").then(function(result){
                    if(result.data.content.length>0){
                        var pid = result.data.content[0].dimId;
                        this.getMenuTypeByPid(pid);
                    }

                }.bind(this));
            },
            getMenuTypeByPid:function(pid){
                axios.get(kunApis.systemDim+"?level=2&pid="+pid).then(function(result){
                    this.menuTypeList=result.data.content;
                }.bind(this));
            },
            getMenuIcon:function(){
                axios.get(kunApis.systemDim+"?level=1&filter=menuIcon").then(function(result){
                    if(result.data.content.length>0){
                        var pid = result.data.content[0].dimId;
                        this.getMenuIconByPid(pid);
                    }
                }.bind(this));
            },
            getMenuIconByPid:function(pid){
                axios.get(kunApis.systemDim+"?level=2&pid="+pid).then(function(result){
                    this.menuIconList=result.data.content;
                }.bind(this));
            },
            cancel:function(){
                this.resetMenuForm();
                this.dialogVisible=false;
            },
            saveMenu:function(){
                this.menuFormData.rightCode =this.menuFormData.moduleName.hashCode();
                if(this.menuFormData.moduleId != null){
                    axios.put(kunApis.menu,this.menuFormData).then(function(result){
                        this.$message({type: 'success',message: '菜单更新成功!'});
                        this._addM(this.menus,result.data);
                        this.resetMenuForm();
                        this.cancel();
                    }.bind(this)).catch(function(err) {
                        console.log(err)
                    });
                }else{
                    axios.post(kunApis.menu,this.menuFormData).then(function(result){
                        this.$message({type: 'success',message: '菜单添加成功!'});
                        this.getTopMenu();
                        // this._addM(this.menus,result.data); 暂不支持局部刷新子节点
                        this.resetMenuForm();
                        this.cancel();
                    }.bind(this)).catch(function(err) {
                        console.log(err)
                    });
                }
            },
            editMenu:function(menu){
                this.menuFormData = menu;
                this.showDialog();
            },
            deleteMenu:function(menu){
                axios.delete(kunApis.menu+"/"+menu.moduleId).then(function(result){
                    this._deleteM(this.menus,menu);
                    this.$message({type: 'success',message: '删除菜单成功!'});
                }.bind(this));

            },
            _deleteM:function(menus,m){
                var isFind = false;
                for(var i = 0 ; i < menus.length; i++){
                    if(menus[i].moduleCode == m.moduleCode){
                        console.log("find...");
                        menus.splice(i,1);
                        isFind = true;
                        break;
                    }
                }
                if(isFind == false){
                    for(var i = 0 ; i < menus.length; i++){
                        if(menus[i].children&&menus[i].children.length > 0){
                            for(var j = 0 ; j < menus[i].children.length; j++){
                                this._deleteM(menus[i].children,m);
                            }
                        }
                    }
                }
            },
            _addM:function(menus,m){
                var isFind = false;
                for(var i = 0 ; i < menus.length; i++){
                    if(menus[i].moduleCode == m.parentCode){
                        if(menus[i].children != undefined){
                            menus[i].children.push(m);
                        }else{
                            menus[i].children = [m];
                        }
                        isFind = true;
                        break;
                    }
                }
                if(isFind == false){
                    for(var i = 0 ; i < menus.length; i++){
                        if(menus[i].children&&menus[i].children.length > 0){
                            for(var j = 0 ; j < menus[i].children.length; j++){
                                this._addM(menus[i].children,m);
                            }
                        }
                    }
                }
            },
            addSubMenu:function(menu){
                this.resetMenuForm();
                this.menuFormData.parentCode = menu.moduleCode;
                this.showDialog();
            },
            deleteMenu: function(menu) {
                var self = this
                self.$confirm('此操作将删除该菜单及其子菜单, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {
                    axios.delete(kunApis.menu+ '/' + menu.moduleId).then(function(result){
                        self.$message({type: 'success',message: '菜单删除成功!'});
                        self.resetMenuForm();
                        self.getTopMenu()
                    })
                }).catch(function() {
                    self.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            editImg:function(){
                this.showIcons = true;
            },
            chooseImg:function(item){
                this.menuFormData.moduleImg = item.dimCode;
                this.showIcons = false;
            },
            menuTypeFormatter:function(row, column){
                var result = this.menuTypeList.findIndex(function(value, index, arr) {
                    return value.dimCode == row.moduleType;
                });
                if(result == -1){
                    return "主菜单";
                }else{
                    return this.menuTypeList[result].dimName;
                }
            }


        },
        mounted:function() {

        },
            data:function(){
                return {
                    showClose:false,
                    menuFormData:{
                        moduleId:null,
                        moduleCode:null,
                        parentCode:null,
                        moduleName:"",
                        moduleType:"",
                        moduleUrl:"",
                        moduleImg:"fa fa-file-text-o",
                        moduleStatus:"1",
                        remark:"",
                        moduleSeq:0
                    },
                    menus:[{
                        'moduleName': '',
                        'moduleType': null,
                        'moduleUrl': null,
                        'moduleImg': null,
                        'moduleStatus': null,
                        'remark':null
                    }],
                    dialogVisible:false,
                    showIcons:false,
                    formLabelWidth: '120px',
                    menuTypeList:[],
                    menuIconList:[],
                    searchKey:''
                }
            },
        activated:function(){

                messages.$emit('change','权限管理');

            },
            mounted:function(){

            },
            beforeMount:function(){
                this.getMenuType();
                this.getMenuIcon();
                this.getTopMenu();
            }
        }


    );
    module.exports = VueComponent;
})
